const { genResponse } = require("../utils")

/** GET 翻页参数检查中间件 */
module.exports.paginationCheck = async function(ctx, next) {
  const { current, pageSize, prev_last_id } = ctx.query
  const parsedPage = Math.max(1, parseInt(current, 10));
  const parsedPageSize = Math.min(100, Math.max(1, parseInt(pageSize, 10)));

  if (isNaN(parsedPage)) {
    ctx.status = 400
    ctx.body = genResponse(false, null, `current 页码有误：${current}`)
    return
  }
  // else if (!prev_last_id) {
  //   ctx.body = genResponse(false, null, '翻页参数错误：prev_last_id')
  //   return
  // }
  if (isNaN(parsedPageSize)) {
    ctx.status = 400
    ctx.body = genResponse(false, null, `pageSize 每页数量有误：${pageSize}`)
    return
  }
  await next()
}